package com.gombe.credit.app.ui.activity

import android.view.Gravity
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.gombe.credit.app.R
import com.gombe.credit.app.common.CommonActivity
import com.gombe.credit.app.databinding.ActGombeCreditPermissionsPageBinding
import com.gombe.credit.app.event.GombeCreditEventPointType
import com.gombe.credit.app.ui.adapter.AdapterGombeCreditPermission
import com.gombe.credit.app.ui.dialog.DialogGombeCreditTips
import com.gombe.credit.app.utils.GombeCreditDensityUtil
import com.gombe.credit.app.utils.GombeCreditTitleBarUtil
import com.gombe.credit.app.utils.decoration.LinearSpacingItemDecoration
import com.gombe.credit.app.utils.helper.GombeCreditAnalysisHelper
import com.gombe.credit.app.utils.helper.GombeCreditPermissionHelper
import com.gombe.credit.app.utils.shared.GombeCreditSharedHelper
import com.gombe.credit.app.utils.shared.GombeCreditSharedKey
import com.gombe.credit.lib.adapter.GombeCreditOnSingleClickListener
import org.json.JSONObject

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/28 14:04
 * @Annotation：权限申明列表
 */
class ActGombeCreditPermissionList: CommonActivity(){
    lateinit var binding: ActGombeCreditPermissionsPageBinding
    private var permissionAdapter: AdapterGombeCreditPermission? = null

    override fun takeGombeCreditRootView(): View {
        binding = ActGombeCreditPermissionsPageBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initGombeCreditView() {
        GombeCreditTitleBarUtil.setStatusBarLightMode(this, true)
        setGombeCreditTitleBarVisibility(View.VISIBLE)
        setGombeCreditTextTitle(getString(R.string.gombe_credit_permission_title))
        setGombeCreditBackBtnVisibility(View.INVISIBLE)
        initGombeCreditPermissionListView()
        binding.permissionContentLayout.visibility = View.INVISIBLE
    }

    override fun initGombeCreditListener() {
        //客服
        rootViewBinding.rootCustomer.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                showCustomerDialog("Permission Page")
            }
        })
        //拒绝
        binding.btnDeny.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                GombeCreditSharedHelper.addGombeCreditSharedData(GombeCreditSharedKey.GOMBECREDIT_PERMISSIONS_STATUS,-1)
                addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.PERMISSION_DENY,"Permission Page")
                showLaunchIntentPage()
            }
        })
        //同意
        binding.btnAgree.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.PERMISSION_AGREE,"Permission Page")
                if (!binding.permissionReadCheckBox.isChecked){
                    showRequestPermissionTipsDialog()
                    return
                }
                showLaunchIntentPage()
            }
        })
    }

    override fun takeGombeCreditData() {
        takeGombeCreditPermissionDataList()
    }

    /**
     * 初始化权限列表
     */
    private fun initGombeCreditPermissionListView(){
        binding.permissionListView.setHasFixedSize(true)
        binding.permissionListView.isNestedScrollingEnabled = false
        binding.permissionListView.isFocusableInTouchMode = false //不需要焦点
        val topDecoration = GombeCreditDensityUtil.dp2px(16f)
        binding.permissionListView.addItemDecoration(LinearSpacingItemDecoration(topDecoration, topDecoration,topDecoration,topDecoration))
        binding.permissionListView.layoutManager = LinearLayoutManager(this)
        permissionAdapter = AdapterGombeCreditPermission(binding.permissionListView)
        binding.permissionListView.adapter = permissionAdapter
        binding.permissionListView.itemAnimator = DefaultItemAnimator()
    }

    /**
     * 加载权限列表
     */
    private fun takeGombeCreditPermissionDataList(){
        val permissionList = arrayListOf<JSONObject>()
//        val item1 = GombeCreditPermissionHelper.queryGombeCreditPermissionItem1()
//        permissionList.add(item1)

        val item2 = GombeCreditPermissionHelper.queryGombeCreditPermissionItem2()
        permissionList.add(item2)

        val item3 = GombeCreditPermissionHelper.queryGombeCreditPermissionItem3()
        permissionList.add(item3)

        val item4 = GombeCreditPermissionHelper.queryGombeCreditPermissionItem4()
        permissionList.add(item4)

        val item5 = GombeCreditPermissionHelper.queryGombeCreditPermissionItem5()
        permissionList.add(item5)

        val item6 = GombeCreditPermissionHelper.queryGombeCreditPermissionItem6()
        permissionList.add(item6)

        val item7 = GombeCreditPermissionHelper.queryGombeCreditPermissionItem7()
        permissionList.add(item7)

        permissionAdapter?.data = permissionList
        binding.permissionContentLayout.visibility = View.VISIBLE
    }

    /**
     * 提示dialog
     */
    private fun showRequestPermissionTipsDialog(){
        val dialog = DialogGombeCreditTips(this,"", object : DialogGombeCreditTips.IDialogCallback{
            override fun onClick(view: View?) {
                when(view?.id){
                    R.id.buttonConfirm -> {
                        showLaunchIntentPage()
                    }
                    R.id.buttonCancel -> {
                        showLaunchIntentPage()
                    }
                }
            }
        })
        dialog.setCancelable(true)
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()
        dialog.setGombeCreditKindTipsIconResource(R.mipmap.gombe_credit_image_dialog_tips_icon)
        dialog.setGombeCreditKindTipsTitleText(getString(R.string.gombe_credit_dialog_kind_tips_title),getColor(R.color.gombe_credit_ff1d1e20))
        dialog.setGombeCreditKindTipsMsgText(getString(R.string.gombe_credit_permission_dialog_agree_tips))
        dialog.setGombeCreditKindTipsMsgGravity(Gravity.CENTER)
        dialog.isGombeCreditKindTipsDoubleButton(true)
        dialog.setGombeCreditKindTipsButtonCancelText(getString(R.string.gombe_credit_button_cancel))
        dialog.setGombeCreditKindTipsButtonCancelBackgroundResource(getColor(R.color.gombe_credit_FFF9F9F7))
        dialog.setGombeCreditKintTipsButtonConfirmText(getString(R.string.gombe_credit_button_ok))
        dialog.setGombeCreditKindTipsButtonConfirmBackgroundResource(getColor(R.color.theme))
    }

    /**
     * 执行跳转逻辑
     */
    fun showLaunchIntentPage(){
        //是否显示引导页
        if (!GombeCreditSharedHelper.takeGombeCreditSharedBoolean(GombeCreditSharedKey.GOMBECREDIT_SHOW_GUIDE_PAGE)){
            showGombeCreditGuidePage()
            return
        }

        //已登录 - 进入主页
        if (GombeCreditSharedHelper.takeGombeCreditSharedBoolean(GombeCreditSharedKey.GOMBECREDIT_LOGIN)){
            //初始化analysis lib 赋值 用户id 和 token
            GombeCreditAnalysisHelper.initAnalysisLibAtLoginSuccess()
            showGombeCreditMainPage()
            return
        }
        //进入登录页面
        showGombeCreditLoginPage()
    }

    /**
     * 进入引导页
     */
    private fun showGombeCreditGuidePage() {
        onIntent(ActGombeCreditGuide::class.java)
        finish()
    }

    /**
     * 进入登录页
     */
    private fun showGombeCreditLoginPage() {
        onIntent(ActGombeCreditUserLogin::class.java)
        finish()
    }

    /**
     * 进入主页
     */
    private fun showGombeCreditMainPage() {
        onIntent(ActGombeCreditMain::class.java)
        finish()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_PAGE.PERMISSION_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_PAGE.PERMISSION_END)
    }
}